//
//  AlarmSettingViewController.m
//  SIPphone
//
//  Created by wrt on 14-2-18.
//  Copyright (c) 2014年 wrtsoft. All rights reserved.
//

#import "AlarmSettingViewController.h"
#import "SIPInterface.h"


@interface AlarmSettingViewController ()

@end

@implementation AlarmSettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = NSLocalizedString(@"alarm settings", @"报警配置 Alarm settings");
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.]
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    NSString *alarmIPAddressStr = [[NSString alloc] init];
    alarmIPAddressStr = [defaults valueForKey:@"SAVEALARMIPADDRESS"];
    if (alarmIPAddressStr > 0)
    {
        self.alarmIPAddressTextField.text = alarmIPAddressStr;
    }
    
    NSString *alarmIPPortStr = [[NSString alloc] init];
    alarmIPPortStr = [defaults valueForKey:@"SAVEALARMIPPORT"];
    if (alarmIPAddressStr > 0)
    {
        self.alarmIPportTextfield.text = alarmIPPortStr;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)saveButtonPressed:(id)sender
{
    if (self.alarmIPportTextfield.text.length > 0 && self.alarmIPAddressTextField.text.length > 0)
    {
        SIPInterface *interface = [SIPInterface shareSIPInterface];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        
        [interface setAlarmIPAddressAndPort:self.alarmIPAddressTextField.text andPort:self.alarmIPportTextfield.text];
        
        [defaults setObject:self.alarmIPAddressTextField.text forKey:@"SAVEALARMIPADDRESS"];
        [defaults setObject:self.alarmIPportTextfield.text forKey:@"SAVEALARMIPPORT"];
        [defaults synchronize];
        
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"notice", @"通知 notice") message:NSLocalizedString(@"please fill out the complete contents", @"请将内容填写完整 Please fill out the complete contents") delegate:self cancelButtonTitle:NSLocalizedString(@"OK", @"好的 OK") otherButtonTitles: nil];
        [alert show];
    }
}

- (IBAction)alarmIPAddressTextFieldDidEnd:(id)sender
{
    [self.alarmIPAddressTextField resignFirstResponder];
}

- (IBAction)alarmIPPortTextFieldDidEnd:(id)sender
{
    [self.alarmIPportTextfield resignFirstResponder];
}
- (IBAction)backgroundTouchDown:(id)sender
{
    [self.alarmIPAddressTextField resignFirstResponder];
    [self.alarmIPportTextfield resignFirstResponder];
}

@end
